<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-15
 * Time: 下午3:18
 */
namespace Admin\Controller;


use Common\Controller\AdminCommonController;

/**
 * Class TagController
 * @package Admin\Controller
 * 附件配置控制器
 */
class AttachController extends AdminCommonController {
    private $_model;
     function _initialize(){
        parent::_initialize();
        $this->_model= D('AttachDirConfig');
    }
    /**
     * 附件配置的列表页
     */
    public function index(){
        $list = $this->_model->getList();

        $this->assign('type_name',$this->get_type_name());
        $this->assign($list);
        $this->display();

    }

    /**
     * 附件配置添加/更新的显示页面
     */
    public function edit(){
        $id = I('get.id');
        if($id){
            $info = $this->_model->detail($id);
            $this->assign('info',$info);
        }
        $this->assign('type_name',$this->get_type_name());
        $this->display();
    }

    /**
     * 添加或者更新附件配置
     */
    public function update(){
        if(IS_POST){
            $data = $this->returnPostData();

            if($data['id']){
                $msg = '修改';
            }else{
                $msg = '添加';
            }

            if($this->_model->update($data,$data['id']) != false){
                $this->clearFlush();
                $this->addHistoryLog($msg.'附件配置成功！');
                $this->returnData($msg.'成功！',1,U('Attach/index'));
            }else{
                $this->returnData($msg.'失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    /**
     * 获取返回数据
     * @return array
     */
    private function returnPostData(){
        $id = I('post.id');
        $alias = I('post.alias');
        $app_name = I('post.app_name');
        $dir_path = I('post.dir_path');
        $extension = I('post.extension');
        $max_size = I('post.max_size');
        $type = I('post.type');
        $extend = I('post.extend');

        empty($alias) && $this->returnData('别名不为空！');
        if( abslength($alias) > 50){
            $this->returnData('别名长度不能超过50个字符！');
        }

        if($this->_model->checkFieldExists('alias',$alias,$id)){
            $this->returnData('该别名已存在！');
        }

        empty($app_name) && $this->returnData('应用名称不为空！');
        if(abslength($app_name) > 30){
            $this->returnData('应用名称不能超过30个字符！');
        }

        empty($dir_path) && $this->returnData('存放目录不为空！');
        if(abslength($dir_path) > 50){
            $this->returnData('存放目录不超过50个字符');
        }
        empty($extension) && $this->returnData('扩展不为空！');
        empty($max_size) && $this->returnData('最大值不为空！');

        $data = array(
            'id'=>$id,
            'alias'=>$alias,
            'app_name'=>$app_name,
            'dir_path'=>$dir_path,
            'extension'=>$extension,
            'max_size'=>$max_size,
            'type'=>$type,
            'extend'=>$extend,
        );

        unset($where);
        return $data;
    }
    /**
     * 删除附件配置
     */
    public function del(){
        if(IS_GET){
            $id = I('get.id');
            empty($id) && $this->returnData('请选择操作项！');

            if($this->_model->del($id) != false){
                unset($where);
                $this->clearFlush();
                $this->addHistoryLog('删除附件配置成功！');
                $this->returnData('删除成功！',1);
            }else{
                $this->returnData('删除失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    private function clearFlush(){
        $list = M('attach_dir_config')->select();

        $data = array();
        foreach($list as $val){
            $data[$val['alias']] = $val;
        }

        $localConfigData    = "<?php\n return array(\n'ADJUNC_CONFIG'=>".var_export($data,true)  . ")\n?>";
        $fileName = APP_PATH.'Common/Conf/attachConfig.php';
        file_put_contents($fileName,$localConfigData);

        updateUploadAttach($data);
    }

    /**
     * 获取附件类型
     * @return array
     */
    private function get_type_name(){
        return array('1'=>'图片','2'=>'XLS','3'=>'其它');
    }
}